//==================================================================
//  Copyright (C) 2006-2007  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
/// @file
///
///
/// 
/// @author Davide Pasca
/// 
//==================================================================

#include "stdafx.h"
#include "rend_device.h"

#define USE_AA

#if defined(TARGET_OPENGL)
	#pragma comment( lib, "opengl32.lib" )
#endif

//==================================================================
RendDevice::RendDevice( RendSystemContext *contextp, u_int w, u_int h, const TCHAR *basepathp ) :
	_DrawState(this),
	_is_rendering(false),
	_bloom_onoff(true),
	_bloom_brightness(3.0f),
	_bloom_bright_threshold(1.0f)
{
	_rend_special_targetp = NULL;

	_disp_w = w;
	_disp_h = h;

#if defined(TARGET_D3D)
	createRendTargetD3D( contextp, w, h );
#elif defined(TARGET_OPENGL)
//	wglSwapIntervalEXT( 1 );
#endif

	_ShaderMng.Initialize( this, basepathp );
}

//==================================================================
RendTarget	*RendDevice::NewRendTarget( u_int w, u_int h, const RendTarget::CreationParams &params )
{
	RendTarget	*roffp;

	roffp = new RendTarget( w, h, params, this );

	return roffp;
}

//==================================================================
void RendDevice::RenderBegin( RendTarget *special_targetp )
{
	_DrawState.Reset();

	if ( special_targetp )
	{
		_rend_special_targetp = special_targetp;
		special_targetp->BeginDestination();
	}

#ifdef TARGET_D3D
	renderBeginD3D();

#elif defined(TARGET_OPENGL)
	glClearColor( 0, 0, 0, 0 );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

#endif

	if NOT( special_targetp )
		_ShaderMng.BeginRender();

	_is_rendering = true;
}

//==================================================================
void RendDevice::RenderEnd()
{
	_is_rendering = false;

	if ( _rend_special_targetp )
	{
		_rend_special_targetp->EndDestination();


		//_rend_special_targetp->_col_tex._tex_ogl_id

		_rend_special_targetp = NULL;
		return;
	}

#if defined(TARGET_D3D)
	renderEndD3DSwap();

#endif

	if NOT( _rend_special_targetp )
		_ShaderMng.EndRender();
}

//==================================================================
#ifdef WIN32
void RendDevice::SwapBuffers( void *hdc )
{
	::SwapBuffers( (HDC)hdc );
}
#endif
